/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package saphre.commands;

import java.util.ArrayList;
import java.util.List;
import saphre.concordance.Concordance;
import saphre.core.Store;
import saphre.core.SuffixArray;
import util.Interval;

/**
 * Class which implements a default keyword search.
 * TODO: Needs some heavy refactoring.
 * 
 * @author Niko Schenk
 */
public class CmdKeywordSearch {
    
    Interval paFound;
    Interval saFound;
    
    public void searchForKeyword(Store store, SuffixArray pa, SuffixArray sa, Interval top, Concordance concord, String q) {
        List<String> tokseq = new ArrayList<String>();
                        
            int[] pattern = new int[tokseq.size()];
            for (int i = 0; i < pattern.length; i++) {
                String saved = tokseq.get(i);
                pattern[i] = store.wordToIntChecked(saved);
            }
           
            Interval found = null;
            int lcp;
            if (pa == null) {
                pa = sa.prefixArray();
            }

            pattern = SuffixArray.maximize(pattern, top, sa, pa, store);

            System.out.println("-> " + store.toString(pattern));
            
            found = pa.revSearch(pattern, top);
            paFound = found;
            lcp = pa.getLcp(found);
            Interval rFound = sa.search(pattern, top);
            saFound = rFound;
            System.out.println("PA: " + found + ", " + "SA " + rFound);

            int len = pa.getLcp(found);
            int fullWordCnt = 0;
            int[] lc = pa.getLcptab();
            for (int i = found.lb(); i <= found.rb(); i++) {
                if (lc[i] == len) {
                    fullWordCnt++;
                }
            }
            System.out.println(fullWordCnt + "/" + found.tf());
            fullWordCnt = 0;
            lc = sa.getLcptab();
            for (int i = rFound.lb(); i <= rFound.rb(); i++) {
                if (lc[i] == len) {
                    fullWordCnt++;
                }
            }
            System.out.println(fullWordCnt + "/" + rFound.tf());


            String target = store.toString(pattern);
            
            concord.setTarget(target);
            System.out.println();
           
            concord.getKwic(pa, sa, q); 
    }
}
